package cn.zhaoyuening.schoolmates.web.general;

import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.zhaoyuening.schoolmates.domain.Article;
import cn.zhaoyuening.schoolmates.service.ArticleService;
import cn.zhaoyuening.schoolmates.utils.JsonUtils;
import cn.zhaoyuening.schoolmates.utils.page.PageBean;

@RestController
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value="articles",method=RequestMethod.GET )
	public String articleCategory(@RequestParam(defaultValue="desc") String order,@RequestParam(value="offset",defaultValue="0")Integer offset
			,@RequestParam(defaultValue="10") Integer limit) {
		
		Long total = articleService.getTotalRecord();
		List<Article> list = articleService.findAll(null, null, offset, limit,"id "+order);
		PageBean<Article> pageBean = new PageBean<>(total, list);
		return JsonUtils.objectToJson(pageBean);
	}
	
	
	@RequestMapping(value="article/{id}",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	public String getAricle(@PathVariable(value="id") Long id){
		if(id==null){
			return "fail";
		}
		Article article = articleService.findById(id);
		String jsonData = JsonUtils.objectToJson(article);
		return jsonData;
	}
}
